package com.example.badminton.controller;

import com.example.badminton.common.Result;
import com.example.badminton.entity.Booking;
import com.example.badminton.service.BookingService;
import com.example.badminton.service.CourtService;
import com.example.badminton.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/booking")
@CrossOrigin
public class BookingController {

    @Autowired
    private BookingService bookingService;

    @Autowired
    private CourtService courtService;

    @PostMapping("/create")
    public Result<Booking> createBooking(@RequestBody Booking booking) {
        Long userId = UserUtils.getCurrentUserId();
        booking.setUserId(userId);
        bookingService.save(booking);
        return Result.success(booking);
    }

    @GetMapping("/list")
    public Result<List<Booking>> getBookingList() {
        Long userId = UserUtils.getCurrentUserId();
        List<Booking> bookings = bookingService.getBookingsByUserId(userId);
        return Result.success(bookings);
    }

    @GetMapping("/{id}")
    public Result<Booking> getBookingById(@PathVariable Long id) {
        Long userId = UserUtils.getCurrentUserId();
        Booking booking = bookingService.getById(id);
        if (booking == null) {
            return Result.error("预订记录不存在");
        }
        
        // 检查权限：只能查看自己的预订记录
        if (!booking.getUserId().equals(userId) && !UserUtils.isAdmin()) {
            return Result.error("无权限查看此预订记录");
        }
        
        return Result.success(booking);
    }

    @PutMapping("/{id}")
    public Result<Booking> updateBooking(@PathVariable Long id, @RequestBody Booking booking) {
        Long userId = UserUtils.getCurrentUserId();
        Booking existingBooking = bookingService.getById(id);
        if (existingBooking == null) {
            return Result.error("预订记录不存在");
        }
        
        // 检查权限：只能修改自己的预订记录
        if (!existingBooking.getUserId().equals(userId) && !UserUtils.isAdmin()) {
            return Result.error("无权限修改此预订记录");
        }
        
        booking.setId(id);
        booking.setUserId(userId);
        bookingService.updateById(booking);
        return Result.success(booking);
    }

    @DeleteMapping("/{id}")
    public Result<String> deleteBooking(@PathVariable Long id) {
        Long userId = UserUtils.getCurrentUserId();
        
        Booking booking = bookingService.getById(id);
        if (booking == null) {
            return Result.error("预订记录不存在");
        }
        
        // 检查权限：只能删除自己的预订记录
        if (!booking.getUserId().equals(userId) && !UserUtils.isAdmin()) {
            return Result.error("无权限删除此预订记录");
        }
        
        bookingService.removeById(id);
        return Result.success("删除成功");
    }

    @GetMapping("/court/{courtId}")
    public Result<List<Booking>> getBookingsByCourtId(@PathVariable Long courtId) {
        List<Booking> bookings = bookingService.getBookingsByCourtId(courtId);
        return Result.success(bookings);
    }

    /**
     * 查询所有球场在某天所有时间段的预定状态
     */
    @GetMapping("/availability")
    public Result<java.util.Map<Long, java.util.Map<String, String>>> getAvailability(@RequestParam String date) {
        return Result.success(bookingService.getAvailabilityByDate(date));
    }

} 